#include "nvic.h"
#include "kernel.h"
#include "rcc_apb_periph.h"

#include "gpio.h"

static struct nvic_irq_hander s_tim2_handler={0,{0},};
static struct nvic_irq_hander s_tim3_handler={0,{0},};
static struct nvic_irq_hander s_tim4_handler={0,{0},};

static struct nvic_irq_hander s_usart1_handler={0,{0},};
static struct nvic_irq_hander s_usart2_handler={0,{0},};
static struct nvic_irq_hander s_usart3_handler={0,{0},};
static struct nvic_irq_hander s_usart4_handler={0,{0},};
static struct nvic_irq_hander s_usart5_handler={0,{0},};


static struct nvic_irq_hander s_exti0_handler={0,{0},};
static struct nvic_irq_hander s_exti1_handler={0,{0},};
static struct nvic_irq_hander s_exti2_handler={0,{0},};
static struct nvic_irq_hander s_exti3_handler={0,{0},};
static struct nvic_irq_hander s_exti4_handler={0,{0},};

static struct nvic_irq_hander s_rtc_handler={0,{0},};

static struct nvic_irq_hander s_adc_handler={0,{0},}; //slj add

void SysTick_Handler(void)
{
    HAL_IncTick();
    os_run();
}

void TIM3_IRQHandler(void)
{
    if(s_tim3_handler.init)
    {
        HAL_TIM_IRQHandler(&s_tim3_handler.tim_hander);
    }
    sj_int8 i=0;
    struct nvic_irq_group *group =s_tim3_handler.irq_group;
    for(i=0;i<MAX_IRQ_CALLBACK_NUM;i++)
    {
        if(group[i].callback!=NULL)
        {
           group[i].callback(group[i].arg);
        }
    }
}

void TIM2_IRQHandler(void)
{
    if(s_tim2_handler.init)
    {
        HAL_TIM_IRQHandler(&s_tim2_handler.tim_hander);
    }
    sj_int8 i=0;
    struct nvic_irq_group *group =s_tim2_handler.irq_group;
    for(i=0;i<MAX_IRQ_CALLBACK_NUM;i++)
    {
        if(group[i].callback!=NULL)
        {
           group[i].callback(group[i].arg);
        }
    }
}

void TIM4_IRQHandler(void)
{
    if(s_tim4_handler.init)
    {
        HAL_TIM_IRQHandler(&s_tim4_handler.tim_hander);
    }
    sj_int8 i=0;
    struct nvic_irq_group *group =s_tim4_handler.irq_group;
    for(i=0;i<MAX_IRQ_CALLBACK_NUM;i++)
    {
        if(group[i].callback!=NULL)
        {
           group[i].callback(group[i].arg);
        }
    }
}

void nvic_set_tim_callback(sj_char id,sj_int8 index,nvic_irq_callback callback,sj_void *arg)
{
    struct nvic_irq_hander *hander=NULL;
    switch(id)
    {
        case 2:
            hander =  &s_tim2_handler;
            break;
        case 3:
            hander =  &s_tim3_handler;
            break;
        case 4:
            hander =  &s_tim4_handler;
            break;
    }

    if(hander!=NULL)
    {
        if(hander->irq_group[index].callback_init==0)
        {
            hander->irq_group[index].callback_init=1;
            hander->irq_group[index].callback=callback;
            hander->irq_group[index].arg=arg;
        }
    }
}

struct nvic_irq_hander *nvic_get_tim_handler(sj_char id)
{
    struct nvic_irq_hander *hander=NULL;
    switch(id)
    {
        case 2:
            hander =  &s_tim2_handler;
            break;
        case 3:
            hander =  &s_tim3_handler;
            break;
        case 4:
            hander =  &s_tim4_handler;
            break;
    }

    return hander;
}

void USART1_IRQHandler(void)
{
    if(s_usart1_handler.init)
    {
        HAL_UART_IRQHandler(&s_usart1_handler.usart_hander);
    }
    struct peripheral_usart *usart=s_usart1_handler.ptr;
    HAL_UART_Receive_IT(&s_usart1_handler.usart_hander,usart->rx_buf,20);
}

void USART2_IRQHandler(void)
{
    if(s_usart2_handler.init)
    {
        HAL_UART_IRQHandler(&s_usart2_handler.usart_hander);
    }
    struct peripheral_usart *usart=s_usart2_handler.ptr;
    HAL_UART_Receive_IT(&s_usart2_handler.usart_hander,usart->rx_buf,20);
}

void USART3_IRQHandler(void)
{
    if(s_usart3_handler.init)
    {
        HAL_UART_IRQHandler(&s_usart3_handler.usart_hander);
    }
    struct peripheral_usart *usart=s_usart3_handler.ptr;
    HAL_UART_Receive_IT(&s_usart3_handler.usart_hander,usart->rx_buf,20);
}

void UART4_IRQHandler(void)
{
    if(s_usart4_handler.init)
    {
        HAL_UART_IRQHandler(&s_usart4_handler.usart_hander);
    }
    struct peripheral_usart *usart=s_usart4_handler.ptr;
    HAL_UART_Receive_IT(&s_usart4_handler.usart_hander,usart->rx_buf,20);
}

void UART5_IRQHandler(void)
{
    if(s_usart5_handler.init)
    {
        HAL_UART_IRQHandler(&s_usart5_handler.usart_hander);
    }
    //sj_kprintf("enter UART5_IRQHandler \r\n");
    struct peripheral_usart *usart=s_usart5_handler.ptr;
    HAL_UART_Receive_IT(&s_usart5_handler.usart_hander,usart->rx_buf,20);
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *husart)		//定义 UART 接收完成回调函数功能
{
    if(husart->Instance == s_usart1_handler.usart_hander.Instance)
    {
         struct peripheral_usart *usart=s_usart1_handler.ptr;
         sj_kprintf("uart 1 callback receive data:%s\r\n", usart->rx_buf);
    }
    else if(husart->Instance == s_usart2_handler.usart_hander.Instance)
    {
        struct peripheral_usart *usart=s_usart2_handler.ptr;
        sj_kprintf("uart2 callback receive data:%s\r\n", usart->rx_buf);
        //HAL_UART_Transmit(husart,usart->rx_buf, 20, 1000);	//将uart2接收到的数据通过usart1发�?�出�?
    }
    else if(husart->Instance == s_usart3_handler.usart_hander.Instance)
    {
         struct peripheral_usart *usart=s_usart3_handler.ptr;
         sj_kprintf("uart3 callback receive data:%s\r\n", usart->rx_buf);
    }
    else if(husart->Instance == s_usart4_handler.usart_hander.Instance)
    {
         struct peripheral_usart *usart=s_usart4_handler.ptr;
         sj_kprintf("uart4 callback receive data:%s\r\n", usart->rx_buf);
    }
    else if(husart->Instance == s_usart5_handler.usart_hander.Instance)
    {
         struct peripheral_usart *usart=s_usart5_handler.ptr;
         sj_kprintf("uart5 callback receive data:%s\r\n", usart->rx_buf);
    }
}

struct nvic_irq_hander *nvic_get_usart_handler(sj_char id)
{
    struct nvic_irq_hander *hander=NULL;
    switch(id)
    {
        case 1:
            hander =  &s_usart1_handler;
            break;
        case 2:
            hander =  &s_usart2_handler;
            break;
        case 3:
            hander =  &s_usart3_handler;
            break;
        case 4:
            hander =  &s_usart4_handler;
            break;
        case 5:
            hander =  &s_usart5_handler;
            break;
    }

    return hander;
}

static int flag = 0; //slj add
void EXTI0_IRQHandler(void)
{
    if(s_exti0_handler.init)
    {
        HAL_EXTI_IRQHandler(&s_exti0_handler.exti_hander);
        if(flag)
        {
            flag=0;
            HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_SET);
        }
        else
        {
            flag=1;
            HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_RESET);
        }
    }
}
void EXTI1_IRQHandler(void)
{
    if(s_exti1_handler.init)
    {
        HAL_EXTI_IRQHandler(&s_exti1_handler.exti_hander);
        if(flag)
        {
            flag=0;
            HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_SET);
        }
        else
        {
            flag=1;
            HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_RESET);
        }
    }
}
void EXTI2_IRQHandler(void)
{
    if(s_exti2_handler.init)
    {
        HAL_EXTI_IRQHandler(&s_exti2_handler.exti_hander);
        if(flag)
        {
            flag=0;
            HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_SET);
        }
        else
        {
            flag=1;
            HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_RESET);
        }
    }
}

void EXTI3_IRQHandler(void)
{
    sj_kprintf("EXTI3_IRQHandler %d \r\n",s_exti3_handler.exti_hander.Line);
    if(s_exti3_handler.init)
    {
        HAL_EXTI_IRQHandler(&s_exti3_handler.exti_hander);
        if(flag)
        {
            flag=0;
            HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_SET);
        }
        else
        {
            flag=1;
            HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_RESET);
        }
    }
}
void EXTI4_IRQHandler(void)
{
    if(s_exti4_handler.init)
    {
        HAL_EXTI_IRQHandler(&s_exti4_handler.exti_hander);
        if(flag)
        {
            flag=0;
            HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_SET);
        }
        else
        {
            flag=1;
            HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_RESET);
        }
    }
}

struct nvic_irq_hander *nvic_get_exti_handler(sj_char id)
{
    struct nvic_irq_hander *hander=NULL;
    switch(id)
    {
        case 0:
            hander =  &s_exti0_handler;
            break;
        case 1:
            hander =  &s_exti1_handler;
            break;
        case 2:
            hander =  &s_exti2_handler;
            break;
        case 3:
            hander =  &s_exti3_handler;
            break;
        case 4:
            hander =  &s_exti4_handler;
            break;
        case 5:

            break;
    }

    return hander;
}

void RTC_IRQHandler(void)
{
    HAL_RTCEx_RTCIRQHandler(&s_rtc_handler.rtc_hander);
}

struct nvic_irq_hander *nvic_get_rtc_handler(sj_char id)
{
    struct nvic_irq_hander *hander=NULL;
    hander =  &s_rtc_handler;
    return hander;
}

struct nvic_irq_hander *nvic_get_adc_handler(sj_char id)
{
    struct nvic_irq_hander *hander=NULL;
    hander =  &s_adc_handler;
    return hander;
}


